package com.youxin.cpcenter.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.email.RequestEmailBean;
import com.youxin.cpcenter.common.log.LogMethod;
import com.youxin.cpcenter.service.EmailService;
import com.youxin.foundation.utils.ObjectUtils;
import com.youxin.foundation.utils.RegexUtils;

/**
 * 短信接口
 * @author yingde.cao
 *
 */
@RestController
public class EmailController extends BaseController{
	@Autowired
	private EmailService emailService;
	
	@LogMethod(id="ent.email",eventContent={})
	@RequestMapping(value = "/api/email", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject sendEmail(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestEmailBean requestBean) {
		try {
			ObjectUtils.requestNonNull(requestBean);
			ObjectUtils.requestNonEmpty(requestBean.getMessage(), "短信信息不能为空");
			ObjectUtils.requestNonEmpty(requestBean.getTo(), "Email不能为空");
			if(!RegexUtils.isEmail(requestBean.getTo()))
			{
				return ResponseObject.userFailure("Email格式不正确");
			}
			return emailService.sendEmail(requestBean);
		} catch (Exception e) {
			return ResponseObject.systemError(e);
		}
	}
}
